﻿using Architecture.API.Application.Command.UserInfoRoleCommand;
using Architecture.Domain;
using Architecture.Domain.Dto.UserInfoRoleDto;
using Architecture.Domain.Service.UserInfoRoleServices;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--用户角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UserInfoRoleManagementController : ControllerBase
    {
        /// <summary>
        /// 用户角色管理服务
        /// </summary>
        protected readonly IUserInfoRoleService _userInfoRoleService;

        /// <summary>
        /// 日志
        /// </summary>
        protected readonly ILogger<UserInfoRoleManagementController> logger;

        /// <summary>
        ///  命令总线
        /// </summary>
        protected readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userInfoRoleService">用户角色管理服务</param>
        /// <param name="logger">日志</param>
        /// <param name="mediator">命令总线</param>
        public UserInfoRoleManagementController(IUserInfoRoleService userInfoRoleService, ILogger<UserInfoRoleManagementController> logger, IMediator mediator)
        {
            _userInfoRoleService = userInfoRoleService;
            this.logger = logger;
            _mediator = mediator;
        }

        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <param name="ids">用户ID集合</param>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AssignRoleToUser(AddUserInfoRole addUserInfoRole)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _userInfoRoleService.AddUserInfoRole(addUserInfoRole);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 通过角色ID用户表中的部门ID
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回部门ID集合</returns>
        [HttpGet]
        public async Task<ApiResult<List<UserInfoRoleDepartmentDto>>> GetDepartmentByRoleId(int roleId)
        {
            ApiResult<List<UserInfoRoleDepartmentDto>> result = new ApiResult<List<UserInfoRoleDepartmentDto>>();
            try
            {
                result.Data=await _userInfoRoleService.GetDepartmentByRoleId(roleId);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 通过角色ID查询用户表中的用户ID
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回用户ID和用户名集合</returns>
        [HttpGet]
        public async Task<ApiResult<object>> GetUserInfoByRoleId(int roleId)
        {
            ApiResult<object> result = new ApiResult<object>();
            try
            {
                result.Data = await _userInfoRoleService.GetUserInfoByRoleId(roleId);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除用户角色
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="userId">用户ID</param>
        /// <returns>返回受影响的行数</returns>
        [HttpGet]
        public async Task<ApiResult<int>> DeletedUserInfoRole(int roleId, int userId)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _userInfoRoleService.DeletedUserInfoRole(roleId, userId);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加用户角色 --MediatR 中介者模式
        /// </summary>
        /// <param name="command">命令对象</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> Creare(UserInfoRoleAddCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 通过角色ID用户表中的部门ID
        /// </summary>
        /// <param name="command">角色ID</param>
        /// <returns>返回部门ID集合</returns>
        [HttpPost]
        public async Task<ApiResult<List<UserInfoRoleDepartmentDto>>> SearchDepartmentByRoleId(UserInfoRoleSearchDepartmentIdCommand command)
        {
            ApiResult<List<UserInfoRoleDepartmentDto>> result = new ApiResult<List<UserInfoRoleDepartmentDto>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 通过角色ID查询用户表中的用户ID
        /// </summary>
        /// <param name="command">角色ID</param>
        /// <returns>返回用户ID和用户名集合</returns>
        [HttpPost]
        public async Task<ApiResult<object>> SearchUserInfoByRoleId(UserInfoRoleSearchUserIdCommand command)
        {
            ApiResult<object> result = new ApiResult<object>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除用户角色
        /// </summary>
        /// <param name="command">用户ID、角色ID</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> Remove(UserInfoRoleRemoveCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
